﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Microsoft.SharePoint;
using SPErrorHandler;
using SPWarmUp;
using System.Configuration;
using SendMail;
using SPEnum;

namespace SPMain
{
    class Program
    {  
        static void Main(string[] args)
        {
            ErrorHandler errHandler = new ErrorHandler();
            errHandler.DeleteFile("ErrorLog.txt");
            errHandler.DeleteFile(ConfigurationManager.AppSettings["ErrorLogFile"]);
            errHandler.DeleteFile("WarmUpLog.txt");
            errHandler.DeleteFile(ConfigurationManager.AppSettings["WarmUpLogFile"]);
            if (CheckAppConfigKey())
            {
                GetInitValues(args);
            }
            else
            {
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["MailNotifications"]))
                {
                    errHandler.WriteLogToFile("--------------------------------------------------------------");
                    errHandler.WriteLogToFile("An error is occured during the reading of the SPAdmin.exe.config file! Missing SPAdmin.exe.config file or a key is null!");
                    errHandler.WriteLogToFile("--------------------------------------------------------------");
                }
                else
                {
                    if (ConfigurationManager.AppSettings["MailNotifications"] == "1")
                    {
                        //Send mail with WarmUp Log and default parameter if an error occured during the reading of the app.config 
                        SendEMail SendMessage = new SendEMail();
                        string bodyText = "An error is occured during the reading of the SPAdmin.exe.config file! Missing SPAdmin.exe.config file or a key is null!";
                        SendMessage.SendMailMessage(bodyText, "WarmUp Config Error", "SPAdmin@web.com", "mail@web.com", "ExchangeServer", "WarmUpLog.txt");
                    }
                }                
            }
        }

        //check if a value exists in app.config
        private static bool CheckAppConfigKey()
        {
            try
            {
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["WarmUpLogFile"])) return false;
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["ErrorLogFile"])) return false;
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["ToMail"])) return false;
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["FromMail"])) return false;
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["SmtpServer"])) return false;                
                return true;                
            }
            catch (Exception ex)
            {
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteLogToFile("--------------------------------------------------------------");
                errHandler.WriteLogToFile(ex.ToString());
                errHandler.WriteLogToFile("--------------------------------------------------------------");
                return false;
            }
        }

        private static void GetInitValues(string[] args)
        {
            if (args.Length == 0)
            {
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteErrorToFile("Missing Parameter!");
            }
            else
            {
                //foreach (string currentArg in args)
                //{
                    //string checkValue = currentArg.ToLower();
                    string checkValue = (args[0].ToString()).ToLower();
                    switch (checkValue)
                    {
                        case "-help":
                            Help();
                            break;
                        case "-warmup":
                            InitWarmUp();
                            break;
                        case "-enum":
                            InitEnum();
                            break;
                        default:
                            Console.WriteLine();
                            Console.WriteLine("Missing operation name or the operation name is invalid.");
                            break;
                    }
                //}
            }
        }

        //Tool Help Information. Call with command line argument: -help
        private static void Help()
        {
            ArrayList arrayHelp = new ArrayList();
            arrayHelp.Add("-------------------------------------------------------------------------------");
            arrayHelp.Add("-warmup: WarmUp local Farm.");
            arrayHelp.Add("-------------------------------------------------------------------------------");
            foreach (string newLine in arrayHelp)
            {
                Console.WriteLine(newLine);
            }
        }

        #region WarmUp
        private static void InitWarmUp()
        {
            WarmUp newWarmUp = new WarmUp();
            newWarmUp.WarmUpUrl();
        }
        #endregion

        #region Enum
        private static void InitEnum()
        {
            EnumFarm newEnumDoc = new EnumFarm();
            newEnumDoc.EnumFarmDoc();
        }
        #endregion
    }
}
